﻿import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OrganizationUnitDto, PasswordComplexitySetting, ProfileServiceProxy, UserRoleDto, UserServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { RequirementsForModelComponentDto,StandardPartsFeatureDto,UsingFormDetailDto, UsingFormServiceProxy,UsingFormState,UsingFormDetailExeInput,RoleUserServiceProxy } from '@shared/service-proxies/service-proxies';
import { AbpSessionService } from '@abp/session/abp-session.service';
@Component({
    selector: 'requirementGetEditComponent',
    templateUrl: './requirement-get_edit.component.html'
})
export class RequirementGetEditComponent extends AppComponentBase {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    canChangeUserName = true;
    isTwoFactorEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.TwoFactorLogin.IsEnabled');
    isLockoutEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.UserLockOut.IsEnabled');
    passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting();

    record: RequirementsForModelComponentDto = new RequirementsForModelComponentDto();
    roles: UserRoleDto[];
    sendActivationEmail = true;
    setRandomPassword = true;
    passwordComplexityInfo = '';
    profilePicture: string;

    allOrganizationUnits: OrganizationUnitDto[];
    memberedOrganizationUnits: string[];
    editFlag = true;
    usingFormDtoStateOption:object[];// 审核状态

    taskNumber:string;
    userName:string;
    userId:string;
    count:number;
    authorityUserOption:Array<object>;
    

    constructor(
        injector: Injector,
        private _userService: UserServiceProxy,
        private _usingFormServiceProxy: UsingFormServiceProxy,
        private _roleUserServiceProxy:RoleUserServiceProxy,
        private _userServiceProxy: UserServiceProxy,
        private _abpSessionService: AbpSessionService,
        private _profileService: ProfileServiceProxy
    ) {
        super(injector);
        this.usingFormDtoStateOption = new Array();
        this.usingFormDtoStateOption.push({value:0,text:UsingFormState[0]});
        this.usingFormDtoStateOption.push({value:1,text:UsingFormState[1]});
        this.usingFormDtoStateOption.push({value:2,text:UsingFormState[2]});
        this.usingFormDtoStateOption.push({value:3,text:UsingFormState[3]});
        this.getAuthorityUserOption();
    }
    getUserName(): void {
        let id:number = this._abpSessionService.userId;
        this._userServiceProxy.getUserForEdit(id) .pipe(finalize(() => { }))
        .subscribe(result=> {
            this.userName = result.user.userName;
            if(this.userName){
                this.save();
            }
        });
    }
        /**
     * 获取有权限的用户
     */
    getAuthorityUserOption() {
        this.authorityUserOption = new Array<object>()
        // 车间审批领导
        this._roleUserServiceProxy.getUsersByRole(0)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.authorityUserOption = result;
            });
    }
    show(_pRequirementsForModelComponentDto?: RequirementsForModelComponentDto): void {
        if (_pRequirementsForModelComponentDto) {
            this.record = _pRequirementsForModelComponentDto;
            if(!this.record.features){
                this.record.features = new  StandardPartsFeatureDto();
            }
            this.count = 0;
        }
        this.active = true;
        this.modal.show();
    }

    onShown(): void {
        //document.getElementById('id').focus();
    }

    save(): void {
        if(!this.userName){
            this.getUserName();
            return;
        }
        this.saving = true;
        if(this.record){
            let usingFormDetailDto = new UsingFormDetailDto();
            usingFormDetailDto.standardPartsDetailId = this.record.standardPartsDetailId;
            usingFormDetailDto.quantityOfUsing = this.count;
            usingFormDetailDto.features = this.record.features;
            usingFormDetailDto.usableRange = this.record.usableRange;
            usingFormDetailDto.standardPartsDetailId = this.record.standardPartsDetailId;
            usingFormDetailDto.taskNumber = this.taskNumber;
            usingFormDetailDto.startedUser = this.userName;
            //usingFormDetailDto.taskNumber = this.taskNumber;

            this._usingFormServiceProxy.createOrUpdateDetailInfo(usingFormDetailDto)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(result => {
                this.submit(result);
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });        
        }
    }
    submit(id): void {
        var usingFormDetailExeInput = new UsingFormDetailExeInput();
        usingFormDetailExeInput.id = id;
        usingFormDetailExeInput.userId = this.userId;
        this._usingFormServiceProxy.toExamine(usingFormDetailExeInput)
        .pipe(finalize(() => { }))
        .subscribe(() => {
            this.notify.info(this.l('SavedSuccessfully'));
            this.close();
            this.modalSave.emit(null);
        });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }

    getAssignedRoleCount(): number {
        return _.filter(this.roles, { isAssigned: true }).length;
    }
}
